package com.googlecode.javatestcomparison.dao;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

import com.googlecode.javatestcomparison.Item;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SearchDao
{
   public static SearchDao getInstance()
   {
      SearchDao dao = new SearchDao();
      
      InputStream stream = dao.getClass().getClassLoader().getResourceAsStream("sqlmap.config");
      SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(stream);
      
      dao.setClient(client);
      
      return dao;
   }

   private SqlMapClient client;

   public SqlMapClient getClient()
   {
      return client;
   }

   public void setClient(SqlMapClient client)
   {
      this.client = client;
   }

   public List<Item> searchForItems(String criteria)
   {
      try
      {
         return client.queryForList("getItems");
      }
      catch (SQLException e)
      {
         throw new RuntimeException("An error occured while executing 'getItems'", e);
      }

   }
}
